<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */
 
class Admin_IndexController extends Zenddn_Controller_Action{
	
	public function init(){
		parent::init();
        $this->loadTemplate('alpona');
        $this->view->headTitle = "dashboard";
	}
	
	public function indexAction(){
		
	}
    
    public function backupToSqlFileAction(){
        $this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout()->disableLayout();
        
        try{
            $model = new Admin_Model_BackupDatabase();
            $str = $model->createSQLFile();
            
            $file = $model->getDbName() . " " . date("Y-m-d H-i-s") . ".sql";
            $filepath = APPLICATION_PATH . "/temp/" . $file;
            
            file_put_contents($filepath, $str);
            
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename="'.basename($file).'"'); 
            header('Content-Transfer-Encoding: binary');
            header('Connection: Keep-Alive');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Pragma: public');
            header('Content-Length: ' . filesize($filepath));
            ob_clean();
            flush();
            readfile($filepath);
            exit;
        }catch(exception $e){
            echo "<pre>";
            print_r("Không ghi được file!");
            echo "</pre>";
        }
    }
    
    public function exportExcelAction(){
		$this->_helper->viewRenderer->setNoRender();
        $this->_helper->layout()->disableLayout();
        
        try{
            $workbook = new PHPExcel;
            
            $request = $this->_request;
            $id = $request->getParam('id', false);
            
            if(!$id){
                return;
            }
            
            $order_model = new Admin_Model_SalesInvoice();
            $data = $order_model->getData($id);
            
            $sheet = $workbook->getActiveSheet();
            $sheet->getStyle("A1")->getFont()->setBold(true);
            $sheet->getStyle("A6:G6")->getFont()->setBold(true);
            $sheet->getStyle("A1:G100")->getFont()->setName("Times New Roman")->setSize(12);
            $sheet->getStyle("A1")->getFont()->setItalic(true);
            $sheet->getStyle("A5")->getFont()->setItalic(true)->setSize(14);
            
            $sheet->getStyle('A5')
                    ->getAlignment()
                    ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                    
            $sheet->getStyle('A6:G6')
                    ->getAlignment()
                    ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            
            $option = array('fill' => array(
                                    'type' => PHPExcel_Style_Fill::FILL_SOLID,
                                    'startcolor' => array(
                                        'rgb' => 'FFFF00',
                                    ),
                                ),);
            $sheet->getStyle("A5")->applyFromArray($option);
            $sheet->freezePane('A7');
            
            $sheet->getDefaultRowDimension()->setRowHeight(20);
            $sheet->getColumnDimension("A")->setWidth(45);
            $sheet->getColumnDimension("g")->setWidth(17);
            
            $sheet->getStyle('a5:g6')->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
            
            $sheet->getStyle('D7:H100')
                    ->getAlignment()
                    ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
            
            $sheet->setCellValue('A1','CÔNG TY TNHH SX-TM MP HÂN VY');
            $sheet->setCellValue('A2','Địa chỉ: 35 Tam Hà, Phường Tam Phú, Quận Thủ Đức, TPHCM');
            $sheet->setCellValue('A3','Điện Thoại: (84-8) 39861237-Fax: (84-8) 37296239');
            $sheet->setCellValue('A4','Điện Thoại tư vấn: 0973.479.839-0938.798.876');
            
            $sheet->mergeCells('A5:G5');
            $sheet->setCellValue('A5','HÓA ĐƠN GỞI HÀNG ' . $data['order']['name']);
            
            $sheet->setCellValue('A6', 'TÊN SẢN PHẨM');
            $sheet->setCellValue('B6', 'NET');
            $sheet->setCellValue('C6', 'MH');
            $sheet->setCellValue('D6', 'SL');
            $sheet->setCellValue('E6', 'ĐƠN GIÁ');
            $sheet->setCellValue('F6', 'CK');
            $sheet->setCellValue('G6', 'THÀNH TIỀN');
            
            $i = 7;
            
            foreach($data['products'] as $row){
                if($row['discount_type'] == 1){
                    $type = "%";
                }else{
                    $type = null;
                    $sheet->getStyle('F' . $i)->getNumberFormat()->setFormatCode("#,##0");
                }
                
                $sheet->setCellValue('A' . $i, $row['name']);
                $sheet->setCellValue('B' . $i, $row['net']);
                $sheet->setCellValue('C' . $i, strtoupper($row['brand']));
                $sheet->setCellValue('D' . $i, $row['quantity']);
                $sheet->setCellValue('E' . $i, $row['price']);
                $sheet->setCellValue('F' . $i, $row['discount'] . $type);
                $sheet->setCellValue('G' . $i, $row['total']);
                
                $sheet->getStyle('E' . $i)->getNumberFormat()->setFormatCode("#,##0");
                $sheet->getStyle('G' . $i)->getNumberFormat()->setFormatCode("#,##0");
                $sheet->getStyle("A$i:G$i")->getBorders()->getAllBorders()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
                $sheet->getStyle("B$i:C$i")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                    
                $i ++;
            }
            
            $i += 2;
            
            $sheet->getStyle("A$i:E$i")->getFont()->setBold(true);
            $sheet->getStyle("A$i")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $sheet->setCellValue("A$i", date("d-m-Y", strtotime($data['order']['date'])));
            $sheet->setCellValue("C$i", "TỔNG TOA HÔM NAY");
            $sheet->setCellValue("F$i", $data['order']['subtotal']);
            $sheet->mergeCells("F$i:G$i");
            $sheet->getStyle("F$i")->getNumberFormat()->setFormatCode("#,##0");
            
            $i += 1;
            
            $sheet->getStyle("A$i:E$i")->getFont()->setBold(true);
            $sheet->setCellValue("C$i", "THANH TOÁN");
            $sheet->setCellValue("F$i", $data['order']['totalpay']);
            $sheet->mergeCells("F$i:G$i");
            $sheet->getStyle("F$i")->getNumberFormat()->setFormatCode("#,##0");
            
            $i += 1;
            
            $sheet->getStyle("A$i:E$i")->getFont()->setBold(true);
            $sheet->setCellValue("C$i", "NỢ CŨ");
            $sheet->setCellValue("F$i", $data['order']['dept']);
            $sheet->mergeCells("F$i:G$i");
            $sheet->getStyle("F$i")->getNumberFormat()->setFormatCode("#,##0");
            
            $i += 1;
            
            $sheet->getStyle("A$i:E$i")->getFont()->setBold(true);
            $sheet->setCellValue("C$i", "TRẢ HÀNG");
            $sheet->setCellValue("F$i", $data['order']['refund']);
            $sheet->mergeCells("F$i:G$i");
            $sheet->getStyle("F$i")->getNumberFormat()->setFormatCode("#,##0");
            
            $i += 1;
            
            $sheet->getStyle("A$i:E$i")->getFont()->setBold(true);
            $sheet->getStyle("A$i")->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $sheet->setCellValue("A$i", "THANH KHƯƠNG");
            $sheet->setCellValue("C$i", "TỔNG CÒN LẠI");
            $sheet->setCellValue("F$i", $data['order']['remaining']);
            $sheet->mergeCells("F$i:G$i");
            $sheet->getStyle("F$i")->getNumberFormat()->setFormatCode("#,##0");
            
            $strConvert = new Zenddn_String_ConvertString();
            $writer = new PHPExcel_Writer_Excel5($workbook);
            $name = $strConvert->convert($data['order']['name']) . "-" . date("d-m-Y", strtotime($data['order']['date'])) . ".xls";
            $writer->save($name);
            
            $options = array('prependBase' => false);
            $this->_helper->_redirector->gotoUrl($this->view->baseUrl("/$name"), $options);
        }catch(Exception $e){
            throw "Không ghi được file!";
        }
	}
}